package com.springboot.controller;

import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorChattingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 本控制台用于医生在线咨询发送消息
 */
@Controller
public class DoctorSendChatController {
    @Autowired
    DoctorChattingListService doctorChattingListService;
    /**
     * 医生在线咨询发送短信
     */
    @CrossOrigin
    @RequestMapping(value = "/doctorSendMessageSubmit")
    public @ResponseBody
    String doctorMakePrescription(@RequestParam("tel") String doctorTel,
                                  @RequestParam("otherTel") String otherTel,
                                  @RequestParam("content") String messageContent)
    {
        System.out.println("医生在线咨询聊天");
        try {
            UserLoginForm doctor=doctorChattingListService.findUserByTel(doctorTel);
            UserLoginForm other=doctorChattingListService.findUserByTel(otherTel);
            SimpleDateFormat dateTime=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
            int doctorId=doctor.getUserId();
            int otherId=other.getUserId();
            ChatRecord chatRecord=new ChatRecord();
            chatRecord.setSenderId(doctorId);
            chatRecord.setReceiverId(otherId);
            chatRecord.setDoctorFlag(0);
            chatRecord.setAssistantFlag(0);
            chatRecord.setPatientFlag(0);
            chatRecord.setAssistantId(null);
            chatRecord.setChatContent(messageContent);
            String currentTime=dateTime.format(new Date());
            chatRecord.setChatTime(dateTime.parse(currentTime));
            chatRecord.setFlag1(1);
            chatRecord.setFlag2(0);
            chatRecord.setReceiverState("未读");
            doctorChattingListService.insertChatRecord(chatRecord);
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
